package com.soker.video.controller;

import com.soker.fan.pojo.FanBarrageCartitem;
import com.soker.user.pojo.User;
import com.soker.video.pojo.Video;
import com.soker.video.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class VideoController {
    @Autowired
    VideoService videoService;
    //查找指定页的所有video
    @RequestMapping("/findAllVideo/{classfication}")
    public String findAllVideo(@PathVariable int classfication,HttpServletRequest req, Model model, HttpSession session){
        session.setAttribute("urll",req.getRequestURI());
        List<Video> allVideo = videoService.findAllVideoByClass(classfication);
        model.addAttribute("allVideo",allVideo);
        return "video/food";
    }
    //查找指定页的所有video2
    @RequestMapping("/findAllVideo2")
    public String findAllVideo2(Model model){
        List<Video> allVideo = videoService.findAllVideo();
        model.addAttribute("allVideo",allVideo);
        model.addAttribute("video","true");
        return "popularRank";
    }
    //查找指定页的所有video3
    @RequestMapping("/findAllVideo3/{vname}")
    public String findAllVideo3(@PathVariable String vname, Model model){
        List<Video> allVideo = videoService.findAllVideoHu(vname);
        model.addAttribute("allVideo",allVideo);
        model.addAttribute("video","true");
        model.addAttribute("huname",vname);
        return "search";
    }
    //查找指定风格的所有video
    @RequestMapping("/findByClassification2/{vclassid}/{vname}")
    public String findByClassification2(@PathVariable String vclassid,@PathVariable String vname, Model model){
        Map map = new HashMap();
        map.put("vname",vname);
        map.put("vclassid",vclassid);
        List<Video> allVideo = videoService.findByClassification2(map);
        model.addAttribute("allVideo",allVideo);
        model.addAttribute("video","true");
        model.addAttribute("huname",vname);
        return "search";
    }
    //查找指定风格的所有video
    @RequestMapping("/findByClassification/{vclassid}")
    public String findByClassification(@PathVariable String vclassid,Model model){
        List<Video> allVideo = videoService.findByClassification(vclassid);
        model.addAttribute("allVideo",allVideo);
        model.addAttribute("video","true");
        return "popularRank";
    }
    //插入历史记录（废弃）
//    @RequestMapping("/tofindVideoByVname/{vname}")
//    public String tofindVideoByVname(@PathVariable String vname, Model model,HttpSession session){
//        Video video = videoService.findVideoByVname(vname);
//        String uid = session.getAttribute("uid").toString();
//        String vid = video.getVid();
//        Map map = new HashMap();
//        map.put("uid",uid);
//        map.put("vid",vid);
//
//        return findVideoByVname(vname,model,session);
//    }
    //按video名查询
    @RequestMapping("/findVideoByVname/{vid}/{isFirst}")
    public String findVideoByVname(@PathVariable String vid, Model model,HttpSession session,@PathVariable boolean isFirst){
        if(isFirst==true){
            //视频播放量+1
            videoService.updateTot(vid);
        }
        Video video = videoService.findVideoByVid(vid);
        model.addAttribute("video",video);
        //查询某条视频是否被某用户点赞过
        Map map = new HashMap();
        map.put("vid",vid);
        map.put("uid",session.getAttribute("uid"));
        //点赞
        String isZanForUser = videoService.isZanForUser(map);
        model.addAttribute("isZan",isZanForUser);
        //投币
        String isBiForUser = videoService.isBiForUser(map);
        model.addAttribute("isBi",isBiForUser);
        //收藏
        String isCangForUser = videoService.isCangForUser(map);
        model.addAttribute("isCang",isCangForUser);
        //评论
        List<FanBarrageCartitem> allBarrage = videoService.findBarrageByVname(map);
        model.addAttribute("allBarrage",allBarrage);
        //up信息
        User up = videoService.findUpByCreator(video.getCreator());
        model.addAttribute("up",up);
        map.put("upid",up.getUid());
        //查询当前登录用户是否关注了当前video的up
        boolean fans = videoService.isFans(map);
        model.addAttribute("isFans",fans);
        //查询当前用户是否有当前记录
        boolean exsitHis = videoService.exsitHis(map);

        //获得当前日期
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        map.put("date",dateString);
        if(exsitHis){
            videoService.exsitUpdate(map);
        }else{
            String nickname = videoService.getNickname(video.getCreator());
            map.put("nickname",nickname);
            videoService.insertHis(map);
        }
        return "video/foodplay";
    }
    //点赞
    @RequestMapping("/updateZanv/{vid}/{uid}")
    public String updateZanv(@PathVariable String vid,@PathVariable String uid,Model model,HttpSession session){
        Map map = new HashMap();
        map.put("vid",vid);
        map.put("uid",uid);
        String isZanForUser = videoService.isZanForUser(map);
        if (isZanForUser==null){
            videoService.insertDefaultZan(map);
        }
        videoService.chanIszan(map);
        videoService.updateZan(vid);
        return findVideoByVname(vid,model,session,false);
    }
    //取消点赞
    @RequestMapping("/cancelZanv/{vid}/{uid}")
    public String cancelZanv(@PathVariable String vid,@PathVariable String uid,Model model,HttpSession session){
        Map map = new HashMap();
        map.put("vid",vid);
        map.put("uid",uid);
        videoService.chanIszan2(map);
        videoService.cancelZan(vid);
        return findVideoByVname(vid,model,session,false);
    }
    //投币
    @RequestMapping("/updateBiv/{vid}/{uid}")
    public String updateBiv(@PathVariable String vid,@PathVariable String uid,Model model,HttpSession session){
        Map map = new HashMap();
        map.put("vid",vid);
        map.put("uid",uid);
        String isBiForUser = videoService.isBiForUser(map);
        if(isBiForUser==null){
            videoService.insertDefaultZan(map);
        }
        videoService.chanIsbi(map);
        videoService.updateBi(vid);
        return findVideoByVname(vid,model,session,false);
    }
    //收藏
    @RequestMapping("/updateCang/{vid}/{uid}")
    public String updateCang(@PathVariable String vid,@PathVariable String uid,Model model,HttpSession session){
        Map map = new HashMap();
        map.put("vid",vid);
        map.put("uid",uid);
        String isCangForUser = videoService.isCangForUser(map);
        if (isCangForUser==null){
            videoService.insertDefaultZan(map);
        }
        videoService.chanIscang(map);
        videoService.updateCang(vid);
        return findVideoByVname(vid,model,session,false);
    }
    //取消收藏
    @RequestMapping("/cancelCang/{vid}/{uid}")
    public String cancelCang(@PathVariable String vid,@PathVariable String uid,Model model,HttpSession session){
        Map map = new HashMap();
        map.put("vid",vid);
        map.put("uid",uid);
        videoService.chanIscang2(map);
        videoService.cancelCang(vid);
        return findVideoByVname(vid,model,session,false);
    }
    //插入评论
    @RequestMapping("/insertBarragev/{vid}/{uid}/{barrage}")
    public String insertBarragev(@PathVariable String vid,@PathVariable int uid,@PathVariable String barrage,
                                Model model,HttpSession session){
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        Map map = new HashMap();
        map.put("vid",vid);
        map.put("uid",uid);
        map.put("barrage",barrage);
        map.put("date",dateString);
        videoService.insertBarrage(map);
        return findVideoByVname(vid,model,session,false);
    }
    //关注
    @RequestMapping("/focusUpv/{upid}/{fansid}/{vid}")
    public String focusUpv(@PathVariable String upid,@PathVariable String fansid,@PathVariable String vid,Model model,HttpSession session){
        Map map = new HashMap();
        map.put("upid",upid);
        map.put("fansid",fansid);
        videoService.focusUp(map);
        videoService.updateUp(upid);
        videoService.updateUp2(fansid);
        return findVideoByVname(vid,model,session,false);
    }
    //取消关注
    @RequestMapping("/cancelFocusv/{upid}/{fansid}/{vid}")
    public String cancelFocusv(@PathVariable String upid,@PathVariable String fansid,@PathVariable String vid,Model model,HttpSession session){
        Map map = new HashMap();
        map.put("upid",upid);
        map.put("fansid",fansid);
        videoService.cancelFocus(map);
        videoService.cancelUp(upid);
        videoService.cancelUp2(fansid);
        return findVideoByVname(vid,model,session,false);
    }
}
